package com.tsing.mapper.impl;

import com.tsing.common.framwork.mapper.CommonDao;
import com.tsing.mapper.DynamicMapper;
import com.tsing.pojo.entity.TlInterestid;
import com.tsing.pojo.entity.TlPost;
import com.tsing.pojo.entity.TlUser;

import java.sql.SQLException;
import java.util.List;

/**
 * @auth 卢孝宇
 * @date 2023/12/13 17:06
 */
public class DynamicMapperImpl extends CommonDao implements DynamicMapper {
    @Override
    public List<TlInterestid> userList(Integer userId) {
        List<TlInterestid> list = null;

        Object[] params = {userId};
        try {
            list = executeQuery(TlInterestid.class, "select * from tl_interestid where followId = ?", params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    @Override
    public List<TlPost> postList(Object[] array) {
        List<TlPost> list = null;
        StringBuilder sql = new StringBuilder("select * from tl_post where userId in(");
        if(array.length == 0) {
            sql.append("null)");
        }
        for (int i = 0; i < array.length; i++) {
            if(i == array.length - 1) {
                sql.append("?) order by createDate desc");
                break;
            }
            sql.append("?,");
        }
        System.out.println(sql.toString());
        try {
            list = executeQuery(TlPost.class, sql.toString(), array);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    @Override
    public Integer countAll(Object[] array) {
        Integer list = null;
        StringBuilder sql = new StringBuilder("select count(*) from tl_post where userId in(");
        if(array.length == 0) {
            sql.append("null)");
        }
        for (int i = 0; i < array.length; i++) {
            if(i == array.length - 1) {
                sql.append("?)");
                break;
            }
            sql.append("?,");
        }
        try {
            list = executeCount( sql.toString(), array);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    @Override
    public Integer collectCount(Long postId) {
        Integer list = null;

        Object[] params = {postId};
        try {
            list = executeCount( "select count(*) from tl_collect where postId = ?", params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }
}
